import router from './router'
import store from './store'
import NProgress from 'nprogress' // Progress 进度条
import 'nprogress/nprogress.css' // Progress 进度条样式
import { getToken } from '@/utils/auth' // 验权
import Toast from 'muse-ui-toast';

Toast.config({
    // top / top-start / top-end / bottom / bottom-start / bottom-end
    position: 'top', // 弹出的位置
    time: 2000, // 显示的时长
    closeIcon: 'close', // 关闭的图标
    close: true, // 是否显示关闭按钮
    successIcon: 'check_circle', // 成功信息图标
    infoIcon: 'info', // 信息信息图标
    warningIcon: 'priority_high', // 提醒信息图标
    errorIcon: 'warning' // 错误信息图标
})

const whiteList = [ '/login', '/logindo', '/register', '/404' ] //不重定向白名单
const notNeedLogin = [ '/login', '/logindo', '/register', '/404' ] //不需要登录白名单
router.beforeEach((to, from, next) => {
    NProgress.start();
    if (getToken()) {
        if (notNeedLogin.includes(to.path)) {
            next({
                path: '/TimeCaps'
            })
            NProgress.done() // if current page is dashboard will not trigger	afterEach hook, so manually handle it
        } else {
            if (!store.getters.username) {
                store.dispatch('GetInfo').then(res => { // 拉取用户信息
                    next()
                }).catch((err) => {
                    // store.dispatch('FedLogOut').then(() => {
                    //     next({
                    //         path: '/login'
                    //     })
                    // })
                    console.log(err)
                })
            } else {
                next()
            }
        }
    } else {
        if (whiteList.indexOf(to.path) !== -1) {
            next()
        } else {
            // let redirectQuery;
            // to.fullPath == "/" ? redirectQuery = "" : redirectQuery = `?redirect=${to.fullPath}`;
            next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
            NProgress.done()
        }
    }
})

router.afterEach(() => {
    NProgress.done() // 结束Progress
})
